package com.manpower.adapter;

import java.util.ArrayList;

import com.manpower.data.FileListItemData;
import com.manpower.ui.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

public class FileListAdapter extends BaseAdapter {

	private Context mContext;
	private ArrayList<FileListItemData> mDatas;
	private TextView mFileName = null;
	private CheckBox mFileCheckBox = null;

	public FileListAdapter(Context context, ArrayList<FileListItemData> datas) {
		mContext = context;
		mDatas = datas;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mDatas.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mDatas.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		Holder holder;
		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.file_list_item, null);

			holder = new Holder();
			holder.txtName = (TextView) convertView
					.findViewById(R.id.file_name);
			holder.chkFile = (CheckBox) convertView
					.findViewById(R.id.file_check_box);
			// mFileName.setText(mDatas.get(position).getmFileName());

			convertView.setTag(holder);
		} else
			holder = (Holder) convertView.getTag();

		holder.chkFile
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						mDatas.get(position).setmIsChecked(isChecked);
					}
				});
		holder.txtName = (TextView) convertView.findViewById(R.id.file_name);
		holder.chkFile = (CheckBox) convertView
				.findViewById(R.id.file_check_box);
		holder.txtName.setText(mDatas.get(position).getmFileName());
		holder.chkFile
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						mDatas.get(position).setmIsChecked(isChecked);
					}
				});

		return convertView;
	}

	public class Holder {
		public TextView txtName;
		public CheckBox chkFile;
	}

}
